//
//  ThemeObject.m
//  NvSellerShowSDK
//
//  Created by roc on 17/9/9.
//  Copyright © 2017年 roc. All rights reserved.
//

#import "AssetPackageObject.h"

@implementation AssetPackageObject

- (instancetype)initWithDictionary:(NSDictionary *)objInfo {
    if (self = [super init]) {
        self.assetPackageType = [objInfo valueForKey:@"assetPackageType"];
        self.uuid = [objInfo valueForKey:@"uuid"];
        self.videoScale = [[objInfo valueForKey:@"videoScale"]intValue];
        self.displayName = [objInfo valueForKey:@"displayName"];
        self.imageCoverUrl = [objInfo valueForKey:@"imageCoverUrl"];
        self.previewVideoUrl = [objInfo valueForKey:@"previewVideoUrl"];
        self.zipDownUrl = [objInfo valueForKey:@"zipDownUrl"];
        self.hasPaid = [[objInfo valueForKey:@"hasPaid"]boolValue];
        self.state = [self createStateWith:self.hasPaid];
        self.version = [objInfo valueForKey:@"version"];
        self.fileSavePath = [self createModelSavePath];
        self.themeUpdateUrl = [objInfo valueForKey:@"themeUpdateUrl"];
    }
    return self;
}

- (instancetype)initWithPath:(NSString *)path andAssetId:(NSString *)assetId andType:(NvSSVideoCreateScale)type {
    if (self = [super init]) {
        self.assetPackageType = @"Theme";
        self.state = 2;
        self.hasPaid = YES;
        self.fileSavePath = path;
        self.themeUpdateUrl = nil;
        self.zipDownUrl = nil;
        NSString * jsonInfoPath = [path stringByAppendingString:@"/info.json"];
        if([[NSFileManager defaultManager] fileExistsAtPath:jsonInfoPath]){
            NSData *jdata = [[NSData alloc]initWithContentsOfFile:jsonInfoPath];
            NSError *error;
            NSDictionary *jsonObj = [NSJSONSerialization JSONObjectWithData:jdata options:0 error:&error];
            if(error || jsonObj == nil){
                return nil;
            }
        
            NSString *uuid = [jsonObj objectForKey:@"uuid"];
            if(![uuid isEqualToString:assetId])
                return nil;
            self.version = [jsonObj objectForKey:@"version"];
            self.displayName = [jsonObj objectForKey:@"displayname"];
            self.imageCoverUrl = [path stringByAppendingPathComponent:[jsonObj objectForKey:@"cover"]];
            self.previewVideoUrl = [path stringByAppendingPathComponent:[jsonObj objectForKey:@"preview"]];
        }
    }
    return self;
}

- (NvAssetPackageState)createStateWith:(BOOL)paied {
    if (!paied) {
        return NvAssetPackageStatePaiedNot;
    }
    if (![self hasAssetPackage]) {
        return NvAssetPackageStateDownloadNot;
    }
    return NvAssetPackageStateCanUse;
}

- (NSString *)createModelSavePath {
    NSString *scalePath;
    NSString *assetPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"Assets"];
    NSString *typePath = [assetPath stringByAppendingPathComponent:self.assetPackageType];
    if (self.videoScale == NvSSVideoCreateScaleSquare) {
        scalePath = [typePath stringByAppendingPathComponent:@"squareVideo"];
    } else if (self.videoScale == NvSSVideoCreateScaleVertical) {
        scalePath = [typePath stringByAppendingPathComponent:@"verticalVideo"];
    } else {
        scalePath = [typePath stringByAppendingPathComponent:@"horizontalVideo"];
    }
    return scalePath;
}

- (BOOL)hasAssetPackage {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [[self createModelSavePath]stringByAppendingPathComponent:self.uuid];
    return [fileManager fileExistsAtPath:filePath];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"%@-%@-%i",self.uuid,self.assetPackageType,self.state];
}

@end
